﻿
using Digitalmes.Configuration;
using Digitalmes.Model.EquipmentManagement;
using Digitalmes.Models;
using System;
using WalkingTec.Mvvm.Core.Extensions;

namespace Digitalmes.Services
{
    public class AppService : BaseService,ISingleton
    {
        public AppService(IServiceProvider serviceProvider) : base(serviceProvider)
        {

        }

        public async Task StartLine(SysConfig config)
        {
            try
            {
                ///加载线体设备
                var equipments = DC.Set<Equipment>()
                     .Include(x=>x.EquipAlarm_Equipment)
                     .Include(x => x.EquipmentSerial_Equipment)
                     .Include(x => x.EquipmentTag_Equipment)
                     .Include(x => x.Line)
                     .CheckIDs(config.Lines.Select(x => x.LineId.ToString()).ToList(), x => x.LineId)
                     .ToList();




                var equipmentserive = GetService<EquipmentService>();

                foreach (var equipment in equipments)
                {
                   
                    var para = new EquipmentParameter();
                    para.Key=Guid.NewGuid().ToString();
                    para.EquipmentName = equipment.EquipName;
                    para.EquipmentId = equipment.ID;
                    para.DriverType=equipment.DriverType.Value;
                    para.DriverGroup = equipment.DriverGroup.Trim();
                    para.LineId = equipment.Line.ID;
                    para.LineName = equipment.Line.Name;
                    para.IpAddress = equipment.IpAddress;
                    para.Port=equipment.Port;   
                    para.Serials = equipment.EquipmentSerial_Equipment;
                    para.Tags = equipment.EquipmentTag_Equipment.Select(x => x.ToTag(equipment.DriverGroup)).ToList<ISysTag>();
                    para.MonitorTags = TagGroup(para.Tags);

                    if (string.IsNullOrEmpty(para.DriverGroup))
                    {
                        para.DriverGroup=para.Key;  
                    }

                    await equipmentserive.LoadEquipment(para);
                }

                await equipmentserive.LoadNetDriver();

                Log.Information($"系统加载线体共加载{equipments.Count()}台设备");

            }
            catch (Exception ex)
            {
                Log.Error("加载线体失败", ex);
            }


            await Task.CompletedTask;
        }



        private List<ISysTag> TagGroup(List<ISysTag> tags)
        {
            var mtag=new List<ISysTag>();   
            var entrysign=tags.Where(x=>x.TagType== Model.TagDefineEnum.Entry_Sign).FirstOrDefault();
            if (entrysign != null)
            {
                entrysign.TagGroup=tags.Where(x=>x.GroupType== Model.TagGroupEnum.Entry).ToList();   
                mtag.Add(entrysign);
            }
            var exitsign = tags.Where(x => x.TagType == Model.TagDefineEnum.Archive_Sign).FirstOrDefault();
            if (exitsign != null)
            {
                exitsign.TagGroup = tags.Where(x => x.GroupType == Model.TagGroupEnum.Archive).ToList();
                mtag.Add(exitsign); 
            }
            var ngtakesign = tags.Where(x => x.TagType == Model.TagDefineEnum.TakePartIn_Sign).FirstOrDefault();
            if (ngtakesign != null)
            {
                ngtakesign.TagGroup = tags.Where(x => x.GroupType == Model.TagGroupEnum.NG).ToList();
                mtag.Add(ngtakesign);
            }
            return mtag;    
          
        }

    }
}
